<template>
  <div class="user">
    <header class="page-header">
      用户管理
    </header>

    <div class="btn-group">
      <el-button type="primary" >新增用户</el-button>
    </div>

    <el-form :inline="true"   class="userSearch">
      <el-form-item label="查询">
        <el-input v-model="userSearch.name" placeholder="输入店主姓名/手机号"></el-input>
      </el-form-item>
      <el-form-item label="查询">
        <el-select v-model="userSearch.type" @change="userSearchHandle">
          <el-option label="全部" :value="0"> </el-option>
          <el-option label="微信用户" :value="1"> </el-option>
          <el-option label="普通用户" :value="2"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="userSearchHandle">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="user_list" border style="width: 100%">  
      <el-table-column fixed prop="name" label="姓名"> </el-table-column>
      <el-table-column prop="phone" label="手机号"> </el-table-column>
      <el-table-column prop="address" label="地址"> </el-table-column>
      <el-table-column label="用户类别">
        <template slot-scope="scope">
          <span>{{scope.row.openid == '0' ? '普通用户': '微信用户'}}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="300">
        <template slot-scope="scope">
          <el-popover
            @hide="resetUserInfo"
            placement="top-start"
            :title="'修改手机号（'+ scope.row.name +')'"
            width="200"
            trigger="click">
            <el-form :inline="true">
              <el-form-item>
                <el-input v-model="user_detail.name" placeholder="输入名字"></el-input>
              </el-form-item>
              <el-form-item>
                <el-input v-model="user_detail.shortname" placeholder="输入名字简写"></el-input>
              </el-form-item>
              <el-form-item>
                <el-input v-model="user_detail.phone" placeholder="输入手机号"></el-input>
              </el-form-item>
              <el-form-item>
                <el-input v-model="user_detail.address" placeholder="输入地址"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="success" @click="confirmEdit()">确认修改</el-button>
              </el-form-item>
            </el-form>
            <el-button type="text" size="small" slot="reference" @click="user_details(scope.row.id)">修改用户信息</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination class="page-pagination"
      background
      layout="prev, pager, next"
      @current-change="user_list_change"
      :page-size="userSearch.page_size"
      :total="parseInt(total)">
    </el-pagination>
  </div>
</template>

<script>
import { mapGetters } from "vuex"
export default {
  name: "user",
  data() {
    return {
      userSearch: {
        name: "",
        current_page: 1,
        page_size: 1,
        type: 0
      },
      userInfo:{
        name: '',
        shortname: '',
        address: '',
        phone: '',
        id: ''
      }
    };
  },
  computed: {
    ...mapGetters({
      user_list: 'user/user_list',
      total: 'user/total'
    })
  },
  methods: {
    userSearchHandle() {
      this.userSearch.current_page = 1
      this.$store.dispatch('user/query_user', this.userSearch)
    },
    resetUserInfo() {
      this.userInfo = {
        phone: "",
        userId: ""
      }
    },
    user_list_change(current) {
      this.userSearch.current_page = current
      this.$store.dispatch('user/query_user', this.userSearch)
    },
    confirmEdit(id) {
      console.log(this.user_detail)
      this.$store.dispatch('user/modify_user', this.user_detail)
    },
    user_details(id) {
      this.$store.dispatch('user/user_detail', {id: id})
    }
  },
  created() {
    this.userSearchHandle()
  }
};
</script>

<style lang="scss">
.user {
  .btn-group {
    text-align: center;
    margin-top: 20px;
    margin-bottom: 20px;
  }
  .userSearch {
    text-align: center;
  }
}
</style>